package skeleton;

import java.util.HashMap;

public class NetworkLoaderController {

    private static HashMap<Integer, DigitalElement> digitalElementList = new HashMap<Integer, DigitalElement>();

    public static void loadNetworkFromXMLFile(String fileName) throws Exception {
        Logger.Enter("NetworkLoaderController_loadNetworkFromXMLFile args: fileName " + fileName);

        boolean yesno = Prompter.YesNo("Dobjon Error-t a NetworkLoaderController? ");

        if (yesno) {
            ErrorController.signalError("Fajlbetoltes hiba");

        } else {
            //elemek
            boolean more = true;

            while (more) {
                int answer = Prompter.Switch(new String[]{
                            "Switch",
                            "Generator",
                            "ANDGate",
                            "ORGate",
                            "Inverter",
                            "Led",
                            "Error"
                        });

                switch (answer) {
                    case 0:
                        createDigitalElement("0", ElementType.Switch, "");
                        break;
                    case 1:
                        createDigitalElement("1", ElementType.Generator, "");
                        break;
                    case 2:
                        createDigitalElement("2", ElementType.ANDGate, "");
                        break;
                    case 3:
                        createDigitalElement("3", ElementType.ORGate, "");
                        break;
                    case 4:
                        createDigitalElement("4", ElementType.Inverter, "");
                        break;
                    case 5:
                        createDigitalElement("5", ElementType.Led, "");
                        break;
                    case 6:
                        ErrorController.signalError("Hozzaadas hiba");
                        break;
                }
                
                more = Prompter.YesNo("Megegy DigitalElement? ");
            }
            //vezetekek
            more = true;
            //ahhoz hogy a kotes mukodjon kell 2 element
            //egy switch aminek az id ja 0 illetve egy ANDGate aminek az id ja 2
            //ezt bele kell irnunk a doksiba hogy ez hardcoded
            while (more)
            {
                createConnection(0, 2);
                more = Prompter.YesNo("Megegy Vezetek? ");
            }

        }

        NetworkController NC = NetworkController.getInstance();
        NC.DoTopoOrder();

        Logger.Leave("NetworkLoaderController_loadNetworkFromXMLFile");
    }

    public static void createConnection(int output, int input) throws Exception {
        Logger.Enter("NetworkLoaderController_createConnection args: output " + String.valueOf(output) + "; input " + String.valueOf(input));
        //code
        //get the output element
        DigitalElement outElement = digitalElementList.get(Integer.valueOf(output));

        //get the input element, casting has to be valid
        //if it is not then the input value is wrong
        DigitalGate inGate = (DigitalGate)digitalElementList.get(Integer.valueOf(input));

        //create a wire to connect these
        Wire connector = new Wire();

        //initialize wire
        connector.setDigitalValue(Sign.Low);

        //make the connection
        outElement.addOutput(connector);
        inGate.addInput(connector);

        Logger.Leave("NetworkLoaderController_createConnection");
    }

    public static DigitalElement getDigitalElement(int id){
        return digitalElementList.get(Integer.valueOf(id));
    }

    public static void createDigitalElement(String id, ElementType type, String pattern) throws Exception {
        Logger.Enter("NetworkLoaderController_createDigitalElement args: id " + id + "; type " + type.toString() + "; pattern" + pattern);
        NetworkController NC = NetworkController.getInstance();


        if (type.equals(ElementType.Switch))
        {
            Switch sw = new Switch();
            NC.addSwitch(sw);
            digitalElementList.put(Integer.parseInt(id), sw);
        } else if (type.equals(ElementType.Generator))
        {
            Generator gen = new Generator(pattern);
            NC.addGenerator(gen);
            digitalElementList.put(Integer.parseInt(id), gen);
        } else if (type.equals(ElementType.ANDGate))
        {
            ANDGate gate = new ANDGate();
            gate.addObserver(NC);
            NC.addGate(gate);
            digitalElementList.put(Integer.parseInt(id), gate);
        } else if (type.equals(ElementType.ORGate))
        {
            ORGate gate = new ORGate();
            gate.addObserver(NC);
            NC.addGate(gate);
            digitalElementList.put(Integer.parseInt(id), gate);
        } else if (type.equals(ElementType.Inverter))
        {
            Inverter gate = new Inverter();
            gate.addObserver(NC);
            NC.addGate(gate);
            digitalElementList.put(Integer.parseInt(id), gate);
        } else if (type.equals(ElementType.Led))
        {
            Led gate = new Led();
            gate.addObserver(NC);
            NC.addGate(gate);
            digitalElementList.put(Integer.parseInt(id), gate);
        }



        Logger.Leave("NetworkLoaderController_createDigitalElement");
    }
}
